package clientCommunicationHandler;

import java.io.BufferedReader;
import java.io.Console;

public class UserProxyPasswordReader {
	private Console c;
	private BufferedReader in;
	
public UserProxyPasswordReader(){
	this.c = null;
	this.in = null;
}
public String readPassword(){
	String result;
	if(c != null)
		result = readSecuredPassWordFromConsole();
	else{
		result = readPasswordFromBuffer();
	}
	return result;
}

private String readPasswordFromBuffer() {
	String tPass = null;
	while(tPass == null){
		System.out.println("Enter your password:");
		try {
			tPass = in.readLine();
			if(tPass.equals("")||tPass.equals("\n")){
				tPass = null;
				System.out.println("invalid user pass please re-enter your password");	
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tPass = null;
		}
	}
	return tPass;
}

private String readSecuredPassWordFromConsole() {
	String tPass = null;
	char [] oldPassword = null;
    String tempPass = "";
	while(tPass == null){
		try {
			oldPassword = this.c.readPassword("Enter your password: ");
			for(int i =0 ; i<oldPassword.length;i++){
				tempPass += oldPassword[i];
			}
			tPass = tempPass;
			if(tPass.equals("")||tPass.equals("\n")){
				tPass = null;
				System.out.println("invalid user pass please re-enter your password");	
			}
		} catch (Exception e) {
			e.printStackTrace();
			tPass = null;
		}
	}
	return tPass;
}
	public void setConsole(Console c){
		this.c = c;
	}
	
	public void setBuffer(BufferedReader in){
		this.in = in;
	}
}
